package cn.qyl.joha.controller.admin;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.qyl.joha.common.PageConstant;
import cn.qyl.joha.common.PageModel;
import cn.qyl.joha.dto.Order;
import cn.qyl.joha.service.OrderService;
import cn.qyl.joha.service.Impl.OrderServiceImpl;

@Controller
@RequestMapping("/admin/order")
public class OrderController {
	
	OrderService orderService = new OrderServiceImpl();
	
	@RequestMapping("/getOrders")
	public String getOrders(@RequestParam(name="pageIndex",defaultValue="0") Integer pageIndex,
			Order order,ModelMap map){
		PageModel pageModel = new PageModel();
		//设置当前页
		pageModel.setPageIndex(pageIndex);
		//设置总记录数
		int count = orderService.getCountByOrder(order);
		pageModel.setRecordCount(count);
		
		//获取订单列表
		List<Order> orders = orderService.getOrdersByPage(order,pageModel);
		//返回数据
		map.addAttribute("orders",orders);
		map.addAttribute("order",order);
		map.addAttribute("pageModel",pageModel);
		return "order/orderList";
	}
	
	@RequestMapping("/toAddOrders")
	public String toAddOrder() {
		return "order/addOrder";
	}
	
	@RequestMapping("/addOrders")
	public String addOrders(Order order,ModelMap map ){
		order.setCreateDate(new Date());
		order.setSendDate(new Date());
		int count =  orderService.addOrders(order);
		if(count>0){
			map.put(PageConstant.TIP, "添加成功");
		}else{
			map.put(PageConstant.TIP, "添加失败");
		}
		return "order/addOrder";
	}
	
	@RequestMapping("/toOrderUpdate")
	public String toOrderUpdate(){
		
		return "order/orderUpdate";
	}
}
